#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2,json,commands,time,requests

test_url="http://test.update.funtownlife.com:8001/hook/"
#ding_talk_url="https://oapi.dingtalk.com/robot/send?access_token=0f138a9b1ba5840b814335f8bd080a9848655386690307c000f0c2c085028204"
ding_talk_url="https://oapi.dingtalk.com/robot/send?access_token=b5ca9a148c8e899873e6c826263f1c272486a00750ed928a1c4d5a1ddc01f8bb"
fg=0
while True:
	try:
		sta_code = str(urllib2.urlopen(test_url).getcode())
		
	except Exception,e:
	
		if fg==0:
			e=str(e)
			error_info="{'msgtype': 'text','text':{'content':'报警:The test server:8001--" + e + "'},}"
			error_json=json.dumps(error_info)
			req=urllib2.Request(ding_talk_url,error_json)
			req.add_header('Content-Type','application/json')
			urllib2.urlopen(req)
			fg=1
			continue
	else:
		print fg,sta_code
		if sta_code=="200" and fg==1:
			e=str(e)
			back_json="{'msgtype': 'text','text':{'content':'恢复:The test server:8001--" + e + "'},}"
			back_json=json.dumps(back_json)
			req_header={'Content-Type':'application/json'}
			res=requests.post(ding_talk_url,back_json,headers=req_header)
			fg=0
	
	time.sleep(30)

